package com.enba.boot.web.config;

import com.enba.boot.core.base.ShowBootInfo;
import com.enba.boot.web.interceptor.SecurityContextInterceptor;
import com.enba.boot.web.interceptor.SecurityContextInterceptorCallback;
import com.enba.boot.web.properties.WebProperties;
import javax.annotation.PostConstruct;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@ConditionalOnProperty(prefix = "web", name = "enabled", havingValue = "true")
@EnableConfigurationProperties(WebProperties.class)
@Configuration
public class WebMvcConfig implements WebMvcConfigurer, ShowBootInfo {

  private final WebProperties webProperties;
  private final SecurityContextInterceptorCallback callback;

  public WebMvcConfig(WebProperties webProperties, SecurityContextInterceptorCallback callback) {
    this.webProperties = webProperties;
    this.callback = callback;
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry
        .addInterceptor(new SecurityContextInterceptor(webProperties, callback))
        .addPathPatterns("/**")
        .excludePathPatterns(webProperties.getExcludeUrls())
        .order(1000);
  }

  @PostConstruct
  @Override
  public void showBootInfo() {
    moduleMap.put("enba-boot-starter-web", "enba-boot-starter-web模块集成成功");
  }
}
